﻿// See https://aka.ms/new-console-template for more information
using System.Net;
using System.Net.Sockets;
using System.Text;

Console.WriteLine("Hello, World!");

//创建UdpClient实例，指定本地端口号
UdpClient udpClient = new UdpClient();
//指定接收端口为所有IP地址的固定端口
udpClient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
udpClient.Client.Bind(new IPEndPoint(IPAddress.Any, 8888));
//接收消息
while (true)
{
    var client = new IPEndPoint(IPAddress.Any, 0);
    byte[] receiveBytes = udpClient.Receive(ref client);
   
    string receiveStr = Encoding.Unicode.GetString(receiveBytes);

    Console.WriteLine("Received message: {0}", receiveStr);

    //回复ip
    byte[] msg = Encoding.Unicode.GetBytes("192.168.1.1");
    udpClient.Send(msg, msg.Length, client);

}


